﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace 默写
{
    /// <summary>
    /// 对应课文中的每个要求读或认的新的汉字
    /// </summary>
    public class ACharacter
    {
        /// <summary>
        /// 构建函数
        /// </summary>
        public ACharacter(string text)
        {
            Text = text;
            Request = ACharacterRequest.写;
            ImageIndex = ImageIndex.Character;
            Words = new List<AWord>();
        }

        /// <summary>
        /// 构建函数
        /// </summary>
        public ACharacter(string text, ACharacterRequest RorW)
        {
            Text = text;
            Request = RorW;
            ImageIndex = ImageIndex.Character;
            Words = new List<AWord>();
        }

        /// <summary>
        /// 写或认的要求
        /// </summary>
        public ACharacterRequest Request
        {
            get;
            set;
        }

        /// <summary>
        /// 什么字
        /// </summary>
        public String Text
        {
            get;
            set;
        }
        
        /// <summary>
        /// 当前字对应的词组
        /// </summary>
        public List<AWord> Words
        {
            get;
            set;
        }

        /// <summary>
        /// 生字图片索引值
        /// </summary>
        /// <value>3</value>
        public ImageIndex ImageIndex
        {
            get;
            set;
        }

        /// <summary>
        /// 生成TreeNode节点
        /// </summary>
        public TreeNodeExt ToNode()
        {
            TreeNodeExt rtnTN = new TreeNodeExt(Text+"："+Request.ToString(), (int)ImageIndex, (int)ImageIndex);
            rtnTN.Name = Text + ":" + Request.ToString();
            foreach (AWord aword in Words) {
                rtnTN.Nodes.Add(aword.ToNode());
            }
            return rtnTN;
        }

        /// <summary>
        /// 生成当前生字的字符串
        /// </summary>
        public override String ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(Text);
            sb.Append("：");
            foreach (AWord aword in Words) {
                sb.Append(aword.Text);
                sb.Append("；");
            }
            return sb.ToString().Remove(sb.Length - 1);
        }

        /// <summary>
        /// 生成pdf格式内容
        /// </summary>
        public Phrase ToPDF()
        {
            Phrase phrase = new Phrase();
            string fontpath = Environment.GetFolderPath(Environment.SpecialFolder.Fonts);
            BaseFont heiBF = BaseFont.CreateFont(fontpath + @"\simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            Font heifont = new Font(heiBF,18);
            Chunk textchun = new Chunk(Text+"：",heifont);
            phrase.Add(textchun);
            foreach (AWord aword in Words)
            {
                phrase.Add(aword.ToPDF());
            }
            phrase.Add(Chunk.NEWLINE);
            return phrase;

        }

        /// <summary>
        /// 由节点生成ACharacter对象
        /// </summary>
        public static ACharacter FromNode(TreeNodeExt anode)
        {
            String[] strarray = anode.Name.Split(':');
            ACharacterRequest request = (ACharacterRequest)Enum.Parse(typeof(ACharacterRequest), strarray[1], true);
            ACharacter acharacter = new ACharacter(strarray[0], request);
            foreach (TreeNodeExt nodeext in anode.Nodes)
            {
                acharacter.Words.Add(AWord.FromNode(nodeext));
            }
            return acharacter;
        }

        /// <summary>
        /// 由字符串生成ACharacter对象
        /// </summary>
        /// <param name="str">输入字符串：格式为
        /// 0:认或写
        /// 1:生字
        /// <returns>ACharacter对象</returns>
        public static ACharacter FromString(params string[] strarray)
        {
            if ((strarray[0] == "认" || strarray[0] == "写") && (strarray.Length >= 2))
            {
                ACharacterRequest request = (ACharacterRequest)Enum.Parse(typeof(ACharacterRequest), strarray[0], true);
                ACharacter acharacter = new ACharacter(strarray[1], request);
                if (strarray.Length == 3)
                {
                    string[] temparray = strarray[2].Replace("；", ";").Split(';');
                    foreach (string str in temparray)
                    {
                        acharacter.Words.Add(new AWord(str));
                    }
                }
                return acharacter;
            }
            else
            {
                return null;                     
            }
        }
    }
}
